<template>
    <div class="goods-container">

        <transition @before-enter="beforeEnter"
                    @enter="enter"
                    @after-enter="afterEnter">
            <div class="ball" v-show="ball_flag" ref="ball"></div>
        </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <myswiper :lunbotulist="lunbotu"></myswiper>
                </div>
            </div>
        </div>

        <div class="mui-card">
            <div class="mui-card-header">{{goodsinfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:
                        <del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价:<span class="now_price">
                        ￥{{goodsinfo.sell_price}}
                    </span>
                    </p>
                    <p>购买数量:
                        <numbox v-model="buynum" :maxnum="goodsinfo.stock_quantity"></numbox>
                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addtoshopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>


        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{goodsinfo.goods_no}}</p>
                    <p>库存情况：{{goodsinfo.stock_quantity}}</p>
                    <p>上架时间：{{goodsinfo.add_time}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click.native="goDesc">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click.native="goComment">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
    import myswiper from '../subcomponents/swiper.vue'
    import numbox from '../subcomponents/numbox.vue'

    export default {
        data() {
            return {
                // 获取数据id
                id: this.$route.params.id,
                // 轮播图数据
                lunbotu: [],
                // 产品数据
                goodsinfo: {},
                // 购买数量
                buynum: 1,
                // 小球的开启和关闭
                ball_flag: false
            }
        },
        methods: {
            // 轮播图ajax
            getLunbotu() {
                this.$http.get('api/getthumimages/' + this.id).then(res => {
                    if (res.body.status == 0) {
                        res.body.message.forEach(item => {
                            item.img = item.src
                        })
                        this.lunbotu = res.body.message
                    }
                })
            },
            // 产品信息ajax
            getgoodsinfo() {
                this.$http.get('api/goods/getinfo/' + this.id).then(res => {
                    if (res.body.status === 0) {
                        this.goodsinfo = res.body.message[0]
                    }
                    console.log(this.goodsinfo)
                })
            },
            // 进入产品介绍和产品详情
            goDesc() {
                this.$router.push({name: "goodsDesc", params: {id: this.id}})
            },
            goComment() {
                this.$router.push({name: 'goodsComment', params: {id: this.id}})
            },
            // 加入产品购物车函数,包含动画小球的关闭和vuex的传送数据
            addtoshopcar() {
                this.ball_flag = !this.ball_flag
                this.$store.commit('addtocar', {
                    id: this.id,
                    buynum: this.buynum,
                    maxnum: this.goodsinfo.stock_quantity,
                    price: this.goodsinfo.sell_price,
                    selected: true
                })
            },
            // 球动画钩子函数
            beforeEnter(el) {
                el.style.transform = 'translate(0,0)'
            },
            enter(el, done) {

                el.offsetWidth;
                const ballPosition = this.$refs.ball.getBoundingClientRect()
                const badgePosition = document.getElementById("badge").getBoundingClientRect()
                const xDist = badgePosition.left - ballPosition.left
                const yDist = badgePosition.top - ballPosition.top

                el.style.transform = `translate(${xDist}px,${yDist}px)`
                el.style.transition = 'transform 0.5s cubic-bezier(.69,-0.55,.98,.59)'
                done()
            },
            afterEnter() {
                this.ball_flag = !this.ball_flag
            },
            // 本来用于numbox的传出值用于赋值，由于使用了v-model替代所以函数搁置
            // getselentcount(count) {
            //     this.buynum = count
            // },

        },
        created() {
            // 开启时获取轮播图数据和产品列表数据
            this.getLunbotu()
            this.getgoodsinfo()
        },
        components: {
            // 轮播图组件和产品数量input组件
            myswiper, numbox
        }
    }
</script>

<style scoped lang="scss">
    .now_price {
        font-size: 16px;
        font-weight: 700;
        color: red;
    }

    .goods-container {
        background-color: #eee;
        transition: all cubic-bezier();
    }

    .mui-card-footer {
        display: block;

        button {
            margin-top: 10px;
        }
    }

    .ball {
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        z-index: 999;
        top: 390px;
        left: 140px;
    }
</style>